﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml;

namespace Vision.Core.ActivityComponents.Serialization
{
    /// <summary>
    /// 
    /// </summary>
    public class DeserializeObject<T> : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        public InArgument<byte[]> InputFile { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Output")]
        public OutArgument<T> OutputObject { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            using (MemoryStream mStream = new MemoryStream(this.InputFile.Get(context))){
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                this.OutputObject.Set(context, (T)serializer.ReadObject(mStream));                    
            }
        }
    }
}
